<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/article/ArticleUtil.class.php
 *
 * 文章的工具类
 *
 * PHP Version 5
 *
 * @package:   class.article
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: ArticleUtil.class.php,v 1.1 2006-09-24 14:38:33 ghw Exp $
 * @date:      $Date: 2006-09-24 14:38:33 $
 */


//包含需要使用的函数
include_once FUNCTION_PATH.'utf8_substr.fun.php';
include_once FUNCTION_PATH.'ConvertString.fun.php';


class ArticleUtil {

   /**
    * 取得最近更新的文章
    * @param:  &$id 数据库的连接
    * @return: $article 文章的数组
    * @access: public
    * @static
    */
   public static function &getTopArticleList(&$db) {/*{{{*/
      $sql = 'select id, title from article order by id desc limit 30 offset 0';

      $sth = $db->prepare($sql);
      $res = $db->CacheExecute(25, $sth);

      $article = array();
      
      while ( $rows = $res->FetchRow() ) {
         $article[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'stitle'=>utf8_substr($rows['title'], 0, 24)
         );
      }

      return $article;

   }/*}}}*/


   /**
    * 取得相应分类的最新文章
    * @param:  &$DB 数据库连接
    * @param:  $categroy 指定的文章分类
    * @return: $article
    * @access: public
    * @static
    */
   public static function &getTopArticleFromCategory(&$db, $categroy) {/*{{{*/
      $sql = 'select id, title from article where type=? order by id desc limit 10 offset 0';

      $sth = $db->prepare($sql);
      $res = $db->CacheExecute(25, $sth, array($categroy));

      $article = array();
      
      while ( $rows = $res->FetchRow() ) {
         $article[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'stitle'=>utf8_substr($rows['title'], 0, 20)
         );
      }

      return $article;

   }/*}}}*/


   /**
    * 根据相应的文章分类取得相应的文章列表的总数目
    * @param:  &$id  数据库连接
    * @param:  $id   文章分类的id
    * @param:  $pre_page 每一页显示多少条记录
    * @return: $total_ page 文章的总数页数
    * @access: public
    * @static
    */
   public static function getTotalPage(&$db, $id, $pre_page = 100) {/*{{{*/
      $sql = 'select count(*) as num from article where type=?';
      $sth = $db->prepare($sql);
      $res = $db->CacheExecute(30, $sth, array($id));
      $rows = $res->FetchRow();

      $total_page = ceil( $rows['num'] / $pre_page );

      return $total_page;
   }/*}}}*/

   /**
    * 根据给定的偏移条件和给定的文章分类取出我们需要的文章列表
    * @param:  &$db  数据库的连接
    * @param:  $id   文章的分类
    * @param:  $pre_page   每一页上显示多少条记录
    * @param:  $offset_page   偏移多少条记录
    * @return: $article_list  返回给定的文章列表
    * @access: public
    * @static
    */
   public static function &getArticleList(&$db, $id, $pre_page, $offset_page) {/*{{{*/
      $sql = 'select a.id, a.title, a.author, a.submitdate, a.clicks, b.id as type_id, '.
        ' b.tags as type_name  from article as a join article_tags as b on a.type=b.id where a.type=? order by '. 
         ' a.id desc ';

      $sth = $db->prepare($sql);
      $res = $db->CacheSelectLimit(120, $sth, $pre_page, $offset_page, array($id));

      $article_list = array();

      while ( $rows = $res->FetchRow() ) {
         $article_list[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'ctitle'=>utf8_substr($rows['title'], 0, 30).
            '&nbsp;&nbsp;&nbsp;<i>作者</i>:('.$rows['author'].')',
            'clicks'=>$rows['clicks'],
            'type_id'=>$rows['type_id'],
            'type_name'=>$rows['type_name']
          );
      }
      return $article_list;
   }/*}}}*/

   /**
    * 取得所有文章的页数
    * @param:  &$id  数据库连接
    * @param:  $pre_page 每一页显示多少条记录
    * @return: $total_ page 文章的总数页数
    * @access: public
    * @static
    */
   public static function getAllPage(&$db,  $pre_page = 100) {/*{{{*/
      $sql = 'select count(*) as num from article ';
      $sth = $db->prepare($sql);
      $res = $db->CacheExecute(30, $sth);
      $rows = $res->FetchRow();

      $total_page = ceil( $rows['num'] / $pre_page );

      return $total_page;
   }/*}}}*/

   /**
    * 取得所有的文章列表
    * @param:  &$db  数据库的连接
    * @param:  $pre_page   每一页上显示多少条记录
    * @param:  $offset_page   偏移多少条记录
    * @return: $article_list  返回给定的文章列表
    * @access: public
    * @static
    */
   public static function &getAllArticleList(&$db, $pre_page, $offset_page) {/*{{{*/
      $sql = 'select a.id, a.title, a.author, a.submitdate, a.clicks, b.id as type_id, '.
        ' b.tags as type_name  from article as a join article_tags as b on a.type=b.id  order by '. 
         ' a.id desc ';

      $sth = $db->prepare($sql);
      $res = $db->CacheSelectLimit(120, $sth, $pre_page, $offset_page);

      $article_list = array();

      while ( $rows = $res->FetchRow() ) {
         $article_list[] = array(
            'id'=>$rows['id'],
            'title'=>$rows['title'],
            'ctitle'=>utf8_substr($rows['title'], 0, 30).
            '&nbsp;&nbsp;&nbsp;<i>作者</i>:('.$rows['author'].')',
            'clicks'=>$rows['clicks'],
            'type_id'=>$rows['type_id'],
            'type_name'=>$rows['type_name']
          );
      }
      return $article_list;
   }/*}}}*/
   
   /**
    * 验证看文章是否存在。
    * @param:  &$db 数据库的连接
    * @param:  $id   文章的id
    * @return: $isExists 文章是否存在。
    * @access: public
    * @static
    */
   public static function isExists(&$db, $id) {/*{{{*/
      $sql = 'select count(*) as num from article where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      if ( $rows['num'] ) {
         return true;
      } else {
         return false;
      }

   }/*}}}*/
   
   /**
    * 由文章的id取得文章所在分来的信息
    * @param:  &$db, 数据库的连接
    * @param:  $id,  文章的id
    * @return: $catagory_info 数组 包含文章分类的id和名称
    * @access: public
    * @static
    */
   public static function &getCatagoryInfoById(&$db, $id) {/*{{{*/
      $sql = 'select b.id, b.tags as name from article as a join article_tags as b '.
         ' on a.type = b.id where a.id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      $catagory_info = array (
         'id'=>$rows['id'],
         'name'=>$rows['name']
      );

      return $catagory_info;
   }/*}}}*/

   /**
    * 取得文章的内容
    * @param:  &$db 数据库连接
    * @param:  $id 文章的id
    * @return: $article_info 文章信息数组
    * @access: public
    * @static
    */
   public static function &getArticleInfor(&$db, $id) {/*{{{*/
      $sql = 'select id, type, title, author, fromaddr, content, clicks, submitUser, submitDate, keyword '.
         ' from article where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      $article_info = array(
         'id'=>$rows['id'],
         'title'=>$rows['title'],
         'type'=>$rows['type'],
         'author'=>$rows['author'],
         'fromaddr'=>$rows['fromaddr'],
         'content'=>ConvertString1($rows['content'],  ROOT_URL, IMAGE_URL.'express/'),
         'clicks'=>$rows['clicks'],
         'submitUser'=>$rows['submitUser'],
         'submitDate'=>$rows['submitDate'],
         'keyword'=>$rows['keyword'],
         );

      return $article_info;
   }/*}}}*/
   
   /**
    * 取得文章的评论
    * @param:  &$db  数据库的连接
    * @param:  $id   文章的$id
    * @return: $comment 文章的评论数组
    * @access: public
    * @static
    */
   public static function &getArticleComment(&$db, $id) {/*{{{*/
      $sql = 'select id, user, submitDate, fromaddr, content from article_comment where '.
         ' article_id=? order by id asc';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));

      $comment = array();

      while ( $rows = $res->FetchRow() ) {
      $comment[] = array (
         'author' => $rows['user'],
         'submittime' => $rows['submitDate'],
         'fromaddr' => $rows['fromaddr'],
         'content' => ConvertString($rows['content'],  ROOT_URL, IMAGE_URL.'express/'),
      );
      }

      return $comment;
   }/*}}}*/

   /**
    * 更新文章查看的次数
    * @param:  &$db
    * @param:  $id 文章的id
    * @return: NULL
    * @access: public
    * @static
    */
   public static function UpdateClicks(&$db, $id) {
      $sql = 'update article set clicks = clicks + 1 where id=?';
      $sth = $db->prepare($sql);
      $db->Execute($sth, array($id));

   }




}


?>
